﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataSync.Common
{
    class DataTypeHelper
    {
        public static bool CheckType(string Type, string colValue, out string newcolvalue)
        {
            var bResult = false;
            newcolvalue = string.Empty;
            switch (Type.ToUpper())
            {
                case "DATETIME":
                    DateTime t1 = new DateTime();
                    bResult = DateTime.TryParse(colValue, out t1);
                    if (bResult)
                    {
                        newcolvalue = t1.ToString("yyyy-MM-dd HH:mm:ss");
                    }

                    break;
                case "DECIMAL":
                    decimal t2 = 0;
                    bResult = Decimal.TryParse(colValue, out t2);
                    newcolvalue = t2.ToString();
                    break;
                case "DOUBLE":
                    double t3 = 0;
                    bResult = double.TryParse(colValue, out t3);
                    newcolvalue = t3.ToString();
                    break;
                case "INT16":
                    Int16 t4 = 0;
                    bResult = Int16.TryParse(colValue, out t4);
                    newcolvalue = t4.ToString();
                    break;
                case "INT":
                case "INT32":
                    int t5 = 0;
                    bResult = int.TryParse(colValue, out t5);
                    newcolvalue = t5.ToString();
                    break;
                case "LONG":
                case "INT64":
                    long t6 = 0;
                    bResult = long.TryParse(colValue, out t6);
                    newcolvalue = t6.ToString();
                    break;
                default:
                    newcolvalue = colValue;
                    bResult = true;
                    break;
            }

            return bResult;
        }

        public static object ConvertToType(string Type, string colValue)
        {
            if (string.IsNullOrEmpty(colValue))
            {
                return DBNull.Value;
            }
            switch (Type.ToUpper())
            {
                case "DATETIME":
                    return Convert.ToDateTime(colValue);
                case "DECIMAL":
                    return Convert.ToDecimal(colValue);
                case "DOUBLE":
                    return Convert.ToDouble(colValue);
                case "INT16":
                    return Convert.ToInt16(colValue);
                case "INT":
                case "INT32":
                    return Convert.ToInt32(colValue);
                case "LONG":
                case "INT64":
                    return Convert.ToInt64(colValue);
                default:
                    return colValue;
            }
        }
    }
}
